package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.dao.IDictionaryDao;
import com.magicode.cms.dao.IDictionaryTypeDao;
import com.magicode.cms.entiy.base.Dictionary;
import com.magicode.cms.entiy.base.DictionaryType;
import com.magicode.cms.service.IDictionary;
import com.magicode.cms.service.IDictionaryType;
@Service("servicedictionarytype")
public class DictionaryTypeServiceImpl extends BaseServiceImpl<DictionaryType, IDictionaryTypeDao> implements IDictionaryType{
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
			
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("code", ""+entity_alias_name+".code as code ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			gridsel.put("valid", "case when "+entity_alias_name+".valid = 1 then '有效' else '无效' end "
					+ " as valid ");
			return this.queryMapDataList(hql, gridsel, start, size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("code", ""+entity_alias_name+".code as code ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			gridsel.put("valid", "case when "+entity_alias_name+".valid = 1 then '有效' else '无效' end "
					+ " as valid ");
			return this.queryMapDataList(hql, gridsel, 0, 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql ="from " +DictionaryType.class.getName()+" "+entity_alias_name+" where 1=1 ";
		if(null==querywhere || querywhere.isEmpty())return hql;

		if(querywhere.containsKey(field_id)){
			hql+=" and "+ entity_alias_name+"."+field_id+" = '"+querywhere.get(field_id)+"' ";
		}
		if(querywhere.containsKey("code")){
			hql+=" and "+ entity_alias_name+".code like '%"+querywhere.get("code")+"%' ";
		}
		if(querywhere.containsKey("title")){
			hql+=" and "+ entity_alias_name+".title like '%"+querywhere.get("title")+"%' ";
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "名称");
		map.put("width", 300);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "code");
		map.put(json_title, "编码");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "orderCode");
		map.put(json_title, "排序码");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "valid");
		map.put(json_title, "有效性");
		map.put("width", 50);
		beaninfo.add(map);
		
		return beaninfo;
	}	
	
	@Override
	public IDictionaryTypeDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return dictionarydao;
	}
	@Autowired
	@Qualifier("daodictionarytype")
	private IDictionaryTypeDao dictionarydao;
	public IDictionaryTypeDao getDictionarydao() {
		return dictionarydao;
	}
	public void setDictionarydao(IDictionaryTypeDao dictionarydao) {
		this.dictionarydao = dictionarydao;
	}
	
	
}
